package com.car.advertising.activity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.car.advertising.R;
import com.car.advertising.entity.TaskRecordEntity;
import com.car.advertising.util.FormatUtil;
import com.support.widget.layout.adapter.BaseRecyclerViewAdapter;
import com.support.widget.layout.holder.BaseViewHolder;

import java.util.List;

/**
 * Created by hdb on 2017/4/10.
 */

public class TaskRecordAdapter extends BaseRecyclerViewAdapter<TaskRecordEntity, TaskRecordAdapter.TaskRecordHold> {

    private LookDetailClick detailClick;

    public TaskRecordAdapter(Context context, List<TaskRecordEntity> taskRecordBeen) {
        super(context, taskRecordBeen);
    }

    public void setRefreshList(List<TaskRecordEntity> taskRecordBeen) {
        this.mDatas = taskRecordBeen;
        notifyDataSetChanged();
    }

    public void setLookDetailClick(LookDetailClick detailClick) {
        this.detailClick = detailClick;
    }

    @Override
    protected void bindView(TaskRecordHold holder, final int position, TaskRecordEntity model) {
        TaskRecordHold taskRecordHold = (TaskRecordHold) holder;
        taskRecordHold.taskPeriodsTv.setText(model.getTaskTitle());
        taskRecordHold.taskStateTv.setText(getTaskStatus(taskRecordHold.detailsTv, model.getTaskStatus()));
        taskRecordHold.numberTv.setText(model.getCurrentNum() + "/" + model.getPersonLimit());
        taskRecordHold.profitTv.setText(model.getUnitPrice());
        taskRecordHold.cycleTv.setText(model.getTaskCycle());
        String beginTime = FormatUtil.changeTime1(model.getBeginTime().substring(0, 8), "yyyy-MM-dd");
        String finishTime = FormatUtil.changeTime1(model.getFinishTime().substring(0, 8), "yyyy-MM-dd");
        taskRecordHold.termTv.setText(beginTime + " — " + finishTime);
        taskRecordHold.detailsTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                detailClick.onLookClick(position);
            }
        });
    }

    @Override
    public TaskRecordHold onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_task_record, parent, false);
        return new TaskRecordHold(mContext, view, this);
    }

    class TaskRecordHold extends BaseViewHolder {

        public TextView taskPeriodsTv;
        public TextView taskStateTv;
        public TextView numberTv;
        public TextView profitTv;
        public TextView cycleTv;
        public TextView termTv;
        public TextView detailsTv;

        public TaskRecordHold(Context context, View rootView, BaseRecyclerViewAdapter mBaseRecyclerViewAdapter) {
            super(context, rootView, mBaseRecyclerViewAdapter);
            taskPeriodsTv = (TextView) rootView.findViewById(R.id.task_periods);
            taskStateTv = (TextView) rootView.findViewById(R.id.task_state);
            numberTv = (TextView) rootView.findViewById(R.id.bid_number);
            profitTv = (TextView) rootView.findViewById(R.id.bid_profit);
            cycleTv = (TextView) rootView.findViewById(R.id.bid_cycle);
            termTv = (TextView) rootView.findViewById(R.id.bid_term);
            detailsTv = (TextView) rootView.findViewById(R.id.bid_details);
        }
    }

    private String getTaskStatus(TextView detailsTv, String taskStatus) {
//        状态，0：未发布1：进行中:2：验收中3：已结束4：已过期5：已删除
        switch (taskStatus) {
            case "0":
                detailsTv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
                detailsTv.setText("查看详情");
                return "未发布";

            case "1":
                detailsTv.setTextColor(mContext.getResources().getColor(R.color.orange));
                detailsTv.setText("任务签到");
                return "进行中";

            case "2":
                detailsTv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
                detailsTv.setText("查看详情");
                return "验收中";

            case "3":
                detailsTv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
                detailsTv.setText("查看详情");
                return "已结束";

            case "4":
                detailsTv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
                detailsTv.setText("查看详情");
                return "已过期";

            case "5":
                detailsTv.setTextColor(mContext.getResources().getColor(R.color.text_gray));
                detailsTv.setText("查看详情");
                return "已删除";
        }
        return "未发布";
    }

    public interface LookDetailClick {
        void onLookClick(int position);
    }
}
